package com.myedu.tankgame02;

import jdk.nashorn.internal.ir.CallNode;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * @author Little fat D
 * @version 1.0
 */

@SuppressWarnings({"all"})
public class MyPlane extends JPanel implements KeyListener, Runnable {
    //初始化坦克
    private Hero hero = null;
    //敌人坦克较多放入到vector集合
    Vector<EnemyTank> enemyTanks = new Vector();
    private int enemyTankSize = 3;

    public MyPlane() {
        hero = new Hero(200, 300); //创建我方坦克
        for (int i = 0; i < enemyTankSize; i++) { //创建敌方坦克
            EnemyTank et = new EnemyTank(100 * (i + 1), 100);
            et.setDirect(2);
            enemyTanks.add(et);
            //创建敌方坦克子弹
            Shot shot = new Shot(et.getX(), et.getY(), et.getDirect());
            et.shotVector.add(shot);
            new Thread(shot).start();
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 1000, 750);
        //绘制坦克
        drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 0);
        //绘制子弹
        if (hero.getShot() != null && hero.getShot().isShotLive()) {
            g.draw3DRect(hero.getShot().getX(), hero.getShot().getY(), 1, 1, false);
        }
        //绘制敌人坦克
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank et = enemyTanks.get(i);
            //et.setDirect(2);
            if (et.isEtIsLive()) {
                drawTank(et.getX(), et.getY(), g, et.getDirect(), 1);
                //绘制子弹
                for (int j = 0; j < et.shotVector.size(); j++) {
                    Shot shot = et.shotVector.get(j);
                    if (shot.isShotLive()) {
                        g.draw3DRect(shot.getX(), shot.getY(), 1, 1, false);
                    } else {
                        et.shotVector.remove(j);
                    }
                }
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            hero.setDirect(0);
            hero.moveUp();
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            hero.setDirect(1);
            hero.moveRight();
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            hero.setDirect(2);
            hero.moveDown();
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            hero.setDirect(3);
            hero.moveLeft();
        }
        if (e.getKeyCode() == KeyEvent.VK_J) {
            hero.shotEnemyTank();
        }
        //this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    public void hotTank(Shot shot, EnemyTank et) {
        switch (et.getDirect()) {
            case 0:
            case 2:
                if (shot.getX() > et.getX() - 20 && shot.getX() < et.getX() + 20 &&
                        shot.getY() > et.getY() - 30 && shot.getY() < et.getY() + 30) {
                    shot.setShotLive(false);
                    et.setEtIsLive(false);
                }
                break;
            case 1:
            case 3:
                if (shot.getX() > et.getX() - 30 && shot.getX() < et.getX() + 30 &&
                        shot.getY() > et.getY() - 20 && shot.getY() < et.getY() + 20) {
                    shot.setShotLive(false);
                    et.setEtIsLive(false);
                }
                break;
        }
    }

    /**
     * 绘制坦克
     *
     * @param x         坦克x坐标
     * @param y         坦克y坐标
     * @param g         画笔
     * @param direction tank的方向（上下左右）
     * @param tankType  tank的类型（敌我坦克）
     */
    private void drawTank(int x, int y, Graphics g, int direction, int tankType) {
        switch (tankType) {
            case 0: //玩家坦克
                g.setColor(Color.CYAN);
                break;
            case 1: //敌人坦克
                g.setColor(Color.orange);
                break;
        }

        //0:上，1:右，2:下，3:左
        switch (direction) {
            case 0:
                g.fill3DRect(x - 20, y - 30, 10, 60, false);
                g.fill3DRect(x + 10, y - 30, 10, 60, false);
                g.fill3DRect(x - 10, y - 20, 20, 40, false);
                g.fillOval(x - 10, y - 10, 20, 20);
                g.drawLine(x, y, x, y - 30);
                break;
            case 1: //右
                g.fill3DRect(x - 30, y - 20, 60, 10, false);
                g.fill3DRect(x - 30, y + 10, 60, 10, false);
                g.fill3DRect(x - 20, y - 10, 40, 20, false);
                g.fillOval(x - 10, y - 10, 20, 20);
                g.drawLine(x, y, x + 30, y);
                break;
            case 2: //下
                g.fill3DRect(x - 20, y - 30, 10, 60, false);
                g.fill3DRect(x + 10, y - 30, 10, 60, false);
                g.fill3DRect(x - 10, y - 20, 20, 40, false);
                g.fillOval(x - 10, y - 10, 20, 20);
                g.drawLine(x, y, x, y + 30);
                break;
            case 3: //右
                g.fill3DRect(x - 30, y - 20, 60, 10, false);
                g.fill3DRect(x - 30, y + 10, 60, 10, false);
                g.fill3DRect(x - 20, y - 10, 40, 20, false);
                g.fillOval(x - 10, y - 10, 20, 20);
                g.drawLine(x, y, x - 30, y);
                break;
            default:
                System.out.println("暂时未处理");
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //判断坦克是否被击中
            if (hero.getShot() != null && hero.getShot().isShotLive()) {
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank et = enemyTanks.get(i);
                    hotTank(hero.getShot(), et);
                    if (!hero.getShot().isShotLive() && !et.isEtIsLive()) {
                        enemyTanks.remove(i);
                    }
                }
            }
            this.repaint();
        }
    }
}
